import pypdf
import os

paperFolder = "./paper/"
filenames = os.listdir(paperFolder)
txtFile = open("allAbstracts.md", "w+", encoding="utf-8")
for filename in filenames:
    pdfFile = open(paperFolder + filename, 'rb')
    reader = pypdf.PdfReader(pdfFile)
    text = ""
    try:
        for page in reader.pages:
            text += page.extract_text()
        text = text.split('Abstract')[1]
        text = text.split('1Paper ID')[0]
        text = text.replace('\n', '')
        text = '# ' + filename.rstrip('.pdf') + '\n' + text
        txtFile.write(text + '\n\n')
    except Exception as e:
        print(filename, e)
        continue
    pdfFile.close()
txtFile.close()
